package com.bencatlin.modbusdroid.OldVersion;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.bencatlin.modbusdroid.R;
import com.bencatlin.modbusdroid.activities.ModbusDroidActivity;
import com.serotonin.modbus4j.ip.IpParameters;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


/**
 * Created by Yanlutian on 2017/5/21.
 */


public class pay_confirm extends Activity {
    //
    public static final int REQUEST_CODE = 111;
    EditText text1 = null;
    Button confirm_btn = null;
//    private PollModbus mb2 = null;
//    Thread mbThread2 = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        text1 = (EditText)findViewById(R.id.fortest);
        //
        EditText transaction_name = (EditText)findViewById(R.id.InfoOfTransactionName);
        EditText transaction_num = (EditText)findViewById(R.id.InfoOfTransactionNum);
        EditText transaction_price = (EditText)findViewById(R.id.InfoOfTransactionPrice);
        EditText transaction_money = (EditText)findViewById(R.id.InfoOfTransactionMoney);
        //
        Intent intent1 = getIntent();
        Bundle bundle1 = intent1.getExtras();
        String result = bundle1.getString("aaa");
        text1.setText(result);
        //
        //
        String temp_name = "";
        String temp_num = "";
        String temp_price = "";
        String temp_money = "";
        String temp_address = "";
        /*
        temp_address += String.valueOf(result.charAt(1));
        temp_address += String.valueOf(result.charAt(2));
        */
        temp_name += String.valueOf(result.charAt(1));
        temp_name += String.valueOf(result.charAt(2));

        temp_num += String.valueOf(result.charAt(4));
        //temp_num += String.valueOf(result.charAt(5));

        temp_price += String.valueOf(result.charAt(6));
        temp_price += String.valueOf(result.charAt(7));
        //temp_price += String.valueOf(result.charAt(9));
        //temp_price += String.valueOf(result.charAt(10));

        temp_address += String.valueOf(result.charAt(9));
        //temp_address += String.valueOf(result.charAt(13));

        int num = Integer.parseInt( temp_num);
        float price = Float.valueOf( temp_price);
        float total_money = num * price;
        //
        final int _RegOff=Integer.parseInt(temp_address);
        final int _Value=_RegOff;
        //
        transaction_name.setText(temp_name);
        transaction_num.setText(temp_num);
        transaction_price.setText(temp_price);
        transaction_money.setText(String.valueOf(total_money));
        //

/*added begin*/
        confirm_btn=(Button)findViewById(R.id.ConfirmPay_Btn);
        confirm_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                发送modbus数据，完成支付
                //ModbusDroid.instance.startPollingThread();
                //switchRegType(regType);
                //
                int _slaveAddress=1;
                int _regType=3;
                int _writeRegOffset=_RegOff;
                int _dataType=2;
                int _mbWriteValue=666;
                ModbusDroid.mb.writeValue(new ModbusMultiLocator (_slaveAddress, _regType, _writeRegOffset, _dataType, 1)
                        , _mbWriteValue);

                //ModbusDroid.instance.startOncePollinTask();
                Intent intent3 = new Intent(pay_confirm.this, Pay_Success.class);
                intent3.putExtra("bbb","支付成功，正在出货");
                startActivity(intent3);
            }
        });
/*added end*/
    }
}
